<html>
<head>
    <title>YUI Test Example</title>
    <link type="text/css" rel="stylesheet" href="../build/logger/assets/logger.css" />     
    <link type="text/css" rel="stylesheet" href="../build/yuitest/assets/testlogger.css" />
    <script type="text/javascript" src="../build/yahoo/yahoo.js"></script>
    <script type="text/javascript" src="../build/dom/dom.js"></script>
    <script type="text/javascript" src="../build/event/event.js"></script>
    <script type="text/javascript" src="../build/logger/logger.js"></script>
    <script type="text/javascript" src="../build/yuitest/yuitest-beta.js"></script>
</head>
<body>
    <h1>YUI Test Example</h1>
    <h2>ArrayAssert</h2>
    <p>This example shows how to use the <code>ArrayAssert</code> object to specifically
    interrogate arrays for information about what is contained within. Note that these
    methods can be used on actual <code>Array</code> objects as well as array-like
    objects such as <code>arguments</code> and <code>HTMLCollection</code>.</p> 
    <p>View the source of this page to see how it works.</p>
    <p><input type="button" value="Run Tests" id="btnRun" /></p>
    
    <script type="text/javascript">
    
        var testCase = new YAHOO.tool.TestCase({

            //the name of the test case - if not provided, one is automatically generated
            name: "Array Tests",
            
            //-------------------------------------------------------------------------
            // Setup and teardown
            //-------------------------------------------------------------------------
        
            /*
             * The setUp() method is used to setup data that necessary for a test to
             * run. This method is called immediately before each test method is run,
             * so it is run as many times as there are test methods.
             */
            setUp : function () {
            
                this.data = new Array (0,1,2,3,4,5);
            
            },
            
            /*
             * The tearDown() method is used to clean up the initialization that was done
             * in the setUp() method. Ideally, it should free up all memory allocated in
             * setUp(), anticipating any possible changes to the data. This method is called
             * immediately after each test method is run.
             */
            tearDown : function () {
                delete this.data;
            },
            
            //-------------------------------------------------------------------------
            // Basic tests - all method names must begin with "test"
            //-------------------------------------------------------------------------
            
            /*
             * Test the push() method.
             */
            testPush : function() {
            
                //shortcut variables
                var Assert = YAHOO.util.Assert;
                var ArrayAssert = YAHOO.util.ArrayAssert;
            
                //do whatever data manipulation is necessary
                this.data.push(6);
            
                //basic equality assertion - expected value, actual value, optional error message
                Assert.areEqual(7, this.data.length, "Array should have seven items.");
                
                //array-specific assertions
                ArrayAssert.contains(6, this.data, "Array should contain 7.");
                ArrayAssert.isNotEmpty(this.data, "Array should not be empty.");
                ArrayAssert.indexOf(6, this.data, 6, "The value in position 6 should be 6.");

                
                //these two assertions are the same because we're dealing with primitives
                ArrayAssert.itemsAreEqual([0,1,2,3,4,5,6], this.data, "Arrays should be equal.");
                ArrayAssert.itemsAreSame([0,1,2,3,4,5,6], this.data, "Arrays should be equal.");        
                
            },
            
            /*
             * Test the pop() method.
             */
            testPop : function() {
            
                //shortcut variables
                var Assert = YAHOO.util.Assert;
                var ArrayAssert = YAHOO.util.ArrayAssert;
            
                //do whatever data manipulation is necessary
                var value = this.data.pop();
                
                //array shouldn't be empty
                ArrayAssert.isNotEmpty(this.data, "Array should not be empty.");                
            
                //basic equality assertion - expected value, actual value, optional error message
                Assert.areEqual(5, this.data.length, "Array should have 5 items.");
                Assert.areEqual(5, value, "Value should be 5.");   
                
                //these two assertions are the same because we're dealing with primitives
                ArrayAssert.itemsAreEqual([0,1,2,3,4], this.data, "Arrays should be equal.");
                ArrayAssert.itemsAreSame([0,1,2,3,4], this.data, "Arrays should be equal.");                                
            },
            
            /*
             * Test the reverse() method.
             */
            testReverse : function() {
            
                //shortcut variables
                var Assert = YAHOO.util.Assert;
                var ArrayAssert = YAHOO.util.ArrayAssert;
            
                //do whatever data manipulation is necessary
                this.data = this.data.reverse();
            
                //array shouldn't be empty
                ArrayAssert.isNotEmpty(this.data, "Array should not be empty.");                
                        
                //basic equality assertion - expected value, actual value, optional error message
                Assert.areEqual(6, this.data.length, "Array should have 6 items."); 
                
                //these two assertions are the same because we're dealing with primitives
                ArrayAssert.itemsAreEqual([5,4,3,2,1,0], this.data, "Arrays should be equal.");
                ArrayAssert.itemsAreSame([5,4,3,2,1,0], this.data, "Arrays should be equal.");                                
            },
            
            /*
             * Test the shift() method.
             */
            testShift : function() {
            
                //shortcut variables
                var Assert = YAHOO.util.Assert;
                var ArrayAssert = YAHOO.util.ArrayAssert;
            
                //do whatever data manipulation is necessary
                var value = this.data.shift();
            
                //array shouldn't be empty
                ArrayAssert.isNotEmpty(this.data, "Array should not be empty.");                
            
                //basic equality assertion - expected value, actual value, optional error message
                Assert.areEqual(5, this.data.length, "Array should have 6 items."); 
                Assert.areEqual(0, value, "Value should be 0."); 
                
                //these two assertions are the same because we're dealing with primitives
                ArrayAssert.itemsAreEqual([1,2,3,4,5], this.data, "Arrays should be equal.");
                ArrayAssert.itemsAreSame([1,2,3,4,5], this.data, "Arrays should be equal.");                                
            },            
            
            /*
             * Test the splice() method.
             */
            testSplice : function() {
            
                //shortcut variables
                var Assert = YAHOO.util.Assert;
                var ArrayAssert = YAHOO.util.ArrayAssert;
            
                //do whatever data manipulation is necessary
                var removed = this.data.splice(1, 2, 99, 100);
            
                //basic equality assertion - expected value, actual value, optional error message
                Assert.areEqual(6, this.data.length, "Array should have 6 items.");
                
                //array shouldn't be empty
                ArrayAssert.isNotEmpty(this.data, "Array should not be empty.");                
            
                //the new items should be there
                ArrayAssert.contains(99, this.data, "Array should contain 99.");
                ArrayAssert.contains(100, this.data, "Array should contain 100.");
                ArrayAssert.indexOf(99, this.data, 1, "Value at index 1 should be 99.");   
                ArrayAssert.indexOf(100, this.data, 2, "Value at index 2 should be 100.");   
                
                //these two assertions are the same because we're dealing with primitives
                ArrayAssert.itemsAreEqual([0,99,100,3,4,5], this.data, "Arrays should be equal.");
                ArrayAssert.itemsAreSame([0,99,100,3,4,5], this.data, "Arrays should be equal.");    

                //these two assertions are the same because we're dealing with primitives
                ArrayAssert.itemsAreEqual([1,2], removed, "Removed values should be an array containing 1 and 2.");
                ArrayAssert.itemsAreSame([1,2], removed, "Removed values should be an array containing 1 and 2.");

            },

            /*
             * Test the unshift() method.
             */
            testUnshift : function() {
            
                //shortcut variables
                var Assert = YAHOO.util.Assert;
                var ArrayAssert = YAHOO.util.ArrayAssert;
            
                //do whatever data manipulation is necessary
                this.data.unshift(-1);
            
                //basic equality assertion - expected value, actual value, optional error message
                Assert.areEqual(7, this.data.length, "Array should have 7 items."); 
                
                //array shouldn't be empty
                ArrayAssert.isNotEmpty(this.data, "Array should not be empty."); 

                //the new item should be there
                ArrayAssert.contains(-1, this.data, "Array should contain -1.");
                ArrayAssert.indexOf(-1, this.data, 0, "First item should be -1."); 
            
                //these two assertions are the same because we're dealing with primitives
                ArrayAssert.itemsAreEqual([-1,0,1,2,3,4,5], this.data, "Arrays should be equal.");
                ArrayAssert.itemsAreSame([-1,0,1,2,3,4,5], this.data, "Arrays should be equal.");                                
            } 
            
        });
        
        function runTests(){
            YAHOO.tool.TestRunner.run();
        }
    
        // YAHOO.util.Event.onDomReady(function (){ // need to wait for the frame to load
        YAHOO.util.Event.on(window, "load", function (){
            //create the logger
            var logger = new YAHOO.tool.TestLogger();
            
            //add the tests
            YAHOO.tool.TestRunner.add(testCase);
            
            //add event handler
            YAHOO.util.Event.addListener("btnRun", "click", runTests);
    
            runTests();
        });        
    
    </script>
        


</body>
</html>
